package edu.ccut.saturn.workflow.delegate;

import java.sql.Connection;

import edu.ccut.saturn.component.DefaultComponent;
import edu.ccut.saturn.component.IParameterContext;
import edu.ccut.saturn.component.SaturnData;
import edu.ccut.saturn.operation.Operation;
import edu.ccut.saturn.workflow.common.Constants;
import edu.ccut.saturn.workflow.common.WorkFlowTools;

public class HelpRedo extends DefaultComponent{
	public void execute(IParameterContext parameterContext) throws Exception {
		String workitemId =(String)parameterContext.getParameterValue(0);
		String userId = (String)parameterContext.getParameterValue(1);
		String reason = (String)parameterContext.getParameterValue(2);
		//处理消息表
		Connection conn = parameterContext.getConnection();
		SaturnData delegateMesgInst = WorkFlowTools.getDelegateMesgInstance();
		delegateMesgInst.set("content", reason);
		delegateMesgInst.set("time", Operation.baseService().GetSystemTime());
		delegateMesgInst.set("message_from",userId);
		delegateMesgInst.set("workitem_id", workitemId);
		delegateMesgInst.set("operate_type", Constants.WORKITEM_BIZSTATE_HELP_REDO);//重做
		Operation.dataBase().insert(delegateMesgInst, conn);
		//更新工作项表的业务状态为重做
		SaturnData workitemInst = Operation.dataBase()
				.queryByPrimaryKey(Constants.SATURNDATA_WF_WORKITEM_INSTANCE, workitemId, conn);
		workitemInst.set("bizstate", Constants.WORKITEM_BIZSTATE_HELP_REDO);
		Operation.dataBase().updateByPrimaryKey(workitemInst, "", conn);
		
		//设此工作项的其他参与者的partiintype='N'
		String sql = "update wf_wiparticipant set partiintype='N' where workitemid="+workitemId;
		Operation.dataBase().updateBySQL(sql, conn);
		
		//更新协办者的权限		
		sql = "update wf_wiparticipant set partiintype='Y' where workitemid="+workitemId+" and workitemstate="+Constants.WORKITEM_BIZSTATE_HELP;
		Operation.dataBase().updateBySQL(sql, conn);
		
		//更新代办表的state
		sql = "update wf_delegate set state='"+Constants.DELEGATE_STATE_RUNNING+"' where workitem_id="+workitemId+" and state='"+Constants.DELEGATE_STATE_FINISH+"'";
		Operation.dataBase().updateBySQL(sql, conn);
	}
}
